# Figure 2: Average Turnout by the Number and the Legal Form of the Proposition


# 1. Load Packages ----

library(ggplot2)
library(tidyverse)
library(ggpubr)

# 2. Read in Data ----

load(file = "df_swissvotes.RData")

# 3. Prepare Data ----
# Replace , with .
swissvotes$bet <- gsub(pattern = ",", replacement = ".", x = swissvotes$bet)

# Aggregate Data by the Legal Form of the Proposition
turn_props_type <- swissvotes %>%
  group_by(rechtsform) %>%
  summarise(turnout = mean(as.numeric(bet), na.rm = T),
            se = sd(as.numeric(bet))/sqrt(n())) %>%
  mutate(ci_95 = se*1.96)

# Collapse by Voting Day
swissvotes_summary <- swissvotes %>%
  mutate(ref_obg = ifelse(rechtsform==1, 1, 0),
         ref_fak = ifelse(rechtsform==2, 1, 0),
         init = ifelse(rechtsform==3, 1, 0),
         gegenv = ifelse(rechtsform==4, 1, 0)) %>%
  group_by(datum) %>%
  summarise(no_props = max(anzahl),
            no_ref_obg = sum(ref_obg),
            no_ref_fak = sum(ref_fak),
            no_init = sum(init),
            no_gegenv = sum(gegenv),
            turnout_avg = mean(as.numeric(bet), na.rm=T))

# 4. Create Graph ----
# Average Turnout by the Number of Propositions
turn_props <- swissvotes_summary %>%
  group_by(no_props) %>%
  summarise(turnout = mean(turnout_avg),
            se = sd(turnout_avg, na.rm = T)/sqrt(n())) %>%
  mutate(ci_95 = se*1.96) %>%
  ggplot(data = ., mapping = aes(x = no_props, y = turnout)) +
  geom_point() +
  #  geom_text(mapping = aes(label = paste(round(turnout, digits = 1), "%", sep = "")), 
  #            hjust = -0.1) +
  geom_errorbar(mapping = aes(ymin = turnout-ci_95, ymax = turnout+ci_95)) +
  scale_x_continuous(name = "Number of propositions per voting day",
                     breaks = seq(1,9,1),
                     limits = c(0.5,10)) +
  scale_y_continuous(name = "Average turnout",
                     breaks = seq(34,52,2),
                     labels = paste(seq(34,52,2), "%", sep = ""),
                     limits = c(34,53)) +
  theme_bw()


# Average Turnout by the Legal Form of the Proposition
turn_type <- ggplot(data = turn_props_type, mapping = aes(x = rechtsform, y = turnout)) +
  geom_point() +
  #  geom_text(mapping = aes(label = paste(round(turnout, digits = 1), "%", sep = "")), 
  #            vjust = -1) +
  geom_errorbar(mapping = aes(ymin = turnout-ci_95, ymax = turnout+ci_95),
                width = 0.4) +
  scale_y_continuous(name = "Average turnout",
                     breaks = seq(34,52,2),
                     labels = paste(seq(34,52,2), "%", sep = ""),
                     limits = c(34,53)) +
  scale_x_continuous(name = "",
                     breaks = seq(1,4,1),
                     labels = c("Compulsory \n referendum", "Optional \n referendum",
                                "Popular \n initiative", "Counter \n proposition"),
                     limits = c(0.5,4.5)) +
  theme_bw()

# 5. Save Graph ----
# Final Graph
ggarrange(turn_props, turn_type, ncol = 2, nrow = 1)
ggsave(filename = "Figure2.pdf", width = 7.99, height = 3.45)

